package controller;



import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.apache.commons.lang.Validate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sun.mail.handlers.message_rfc822;
import com.sun.mail.imap.protocol.Status;

import Tools.Sendmail;
import Tools.Sendmessage;
import Tools.Sendtishi;
import Tools.jiami;
import bean.Find;
import bean.Message;
import serviceinterface.interfaceoptions;

@Controller
@RequestMapping(value = "/i")
public class options {
	
	static String yzm;
	
	@Autowired
	private interfaceoptions interfaceoptions;
	
	//管理员审核函数，当管理员点击审核通过或者审核失败按钮的时候会跳转到这里
	@RequestMapping(value = "/t")
	public String status(Message m,Model model) {
		System.out.println(m.getStatus());
		System.out.println(m.getEmail());
		String mesString;
		if (m.getStatus().equals("审核通过")) {
			mesString="审核通过";
		}else {
			mesString="审核失败";
		}
		interfaceoptions.xiugai(m);
		Message message=interfaceoptions.gerenfind(m);
		
		//发送邮件
		try {
			Sendmessage sendmessage = new Sendmessage();
			sendmessage.setuser(m.getEmail(), mesString);
			sendmessage.run();
			model.addAttribute("mes", "操作成功,该用户已经收到"+mesString+"的邮件");
			return "tishi";
		} catch (Exception e) {
			model.addAttribute("mes", "该用户邮箱失效，无法发送提示邮件");
			return "tishi";
		}
		
	}
	
	
	
	//管理员点击查看详情的时候，会跳转到此处，将展示报名者的个人信息
	@RequestMapping(value = "/v")
	public String view(Message m,Model model) {
		Message message=interfaceoptions.gerenfind(m);
		model.addAttribute("tea", message);
		return "newusers";
	}
	
	
	
	//当报名的人审核通过的时候，点击审核通过字样可以跳转到此处，便于打印
	@RequestMapping(value = "/h")
	public String isview(Message m,Model model) {
		Message message=interfaceoptions.gerenfind(m);
		model.addAttribute("tea", message);
		return "isuser";
	}
	
	
	//分页查询，每页两条数据
	@RequestMapping(value = "/f")
	public String find(Message m,Find find,Model model) {
		int count = interfaceoptions.count(m);
		
		if (count%m.getSize()!=0) {
			find.setPage(count/m.getSize()+1);
		}else {
			find.setPage(count/m.getSize());
		}
		m.setStart((m.getStart()-1)*m.getSize());
		m.setFind(find);
		
		List<Message> teachers=interfaceoptions.find(m);
		model.addAttribute("teachers", teachers);
		model.addAttribute("find", find);
		return "admin";
	}
	
	
	
	//用户点击立即报名的时候跳转带此处，进入报名页面
	@RequestMapping(value = "/y")
	public String yemiantiaozhuan(Message m,Model model) {
		model.addAttribute("email", m.getEmail());
		System.out.println("9999999999999999999999999999");
		return "mes";
	}
	
	
	//用户报名上传资料模块，保存图片
	@RequestMapping(value = "/b")
	public String baoming(Message m,Model model) throws IllegalStateException, IOException {
			String staString=interfaceoptions.gerenfind(m).getStatus();
		if (staString.equals("审核失败")||staString.equals("未报名")) {
			//保存数据库的路径
			  String sqlPath = null; 
			  //定义文件保存的本地路径
		      String localPath=System.getProperty("user.dir")+"\\src\\main\\webapp\\pic\\";
		      //定义 文件名
		      String filename=null;  
		      System.out.println(m.getSex());
		      System.out.println(m.getFile());
		      if(!m.getFile().isEmpty()){  
		          //生成uuid作为文件名称  
		          String uuid = m.getId();
		          //获得文件类型（可以判断如果不是图片，禁止上传）  
		          String contentType=m.getFile().getContentType();  
		          //获得文件后缀名 
		          String suffixName=contentType.substring(contentType.indexOf("/")+1);
		          //得到 文件名
		          filename=uuid+"."+suffixName; 
		          //文件保存路径
		          m.getFile().transferTo(new File(localPath+filename));  
		      }
		      //把图片的相对路径保存至数据库
		      sqlPath = "../pic/"+filename;
		      System.out.println(sqlPath);
		      m.setStatus("待审核");
		      m.setFilepath(sqlPath);
		      Sendtishi sendtishi = new Sendtishi();
		      sendtishi.setuser(m.getEmail(), "");
		      sendtishi.run();
		      if (interfaceoptions.baoming(m).equals("ok")) {
		    	  model.addAttribute("teacher", interfaceoptions.gerenfind(m));
				return "user";
			}
		}
		if (staString.equals("审核通过")) {
			model.addAttribute("mes", "您已通过审核，不需要再次发起申请");
			return "tishi";
		}
	      model.addAttribute("mes", "正在审核，请等待邮件消息");
		  return "tishi";
	}
	
	
	
	@RequestMapping(value = "/j")
	public String shan(Message m) {
//		interfaceoptions.shan(m);
		System.out.println(yzm);
		return "ok";
	}
	
	
	
	//用户登陆模块
	@RequestMapping(value = "/d")
	public String denglu(Message m,Model model) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		m.setPwd(jiami.EncoderByMd5(m.getPwd()));
		m=interfaceoptions.denglu(m);
		if (m==null) {
			System.out.println("登陆失败");
			model.addAttribute("mes", "账号或密码错误");
			return "tishi";
		}
		System.out.println(m.getStatus());
		if (m.getStatus().equals("管理员")) {
			return "admin";
		}
		model.addAttribute("teacher", m);
		System.out.println("登陆成功");
		return "user";
		
	}
	
	
	
	//用户注册
	@RequestMapping(value = "/z")
	public String zhuce(Message m,Model model) throws NoSuchAlgorithmException, UnsupportedEncodingException {
		if (m.getYzm().equals(yzm)) {
			m.setPwd(jiami.EncoderByMd5(m.getPwd()));
			m.setStatus("未报名");
				if (interfaceoptions.zhuce(m).equals("ok")) {
					return "denglu";
				}
				else {
					System.out.println("该用户已经注册");
					model.addAttribute("mes", "该用户已经注册");
					return "tishi";
				}
		}else {
			model.addAttribute("mes", "验证码出错");
			return "tishi";
		}
	}
	
	
	
	//用户注册，邮件发送模块
	@RequestMapping(value = "/k")
	public void email(Message m) {
		int i = (int)(Math.random()*900 + 100);
		yzm = Integer.toString(i);
		Sendmail sendmail =new Sendmail();
		sendmail.setuser(m.getEmail(), yzm);
		sendmail.run();
	}
}
